<?php

// +----------------------------------------------------------------------

namespace app\api\controller;

use app\common\controller\Api;
use app\admin\model\Module as ModuleModel;
use app\activity\model\Redpack as RedpackModel;
use app\mall\model\Merch as MerchModel;
use app\merch\model\Child as ChildModel;
use app\mall\model\Order as OrderModel;
use app\merch\model\Daily;
use app\merch\model\Monthly;
use app\api\model\Times;
use app\mall\model\Cert;
use util\Wxsdk;
use wxpay\PayVthree;
use wxpay\GetCert;
use aliutil\Auth as AliPay;
use think\facade\Db;
/**
 * 系统设置Api控制器
 * @package app\api\controller
 */
class Setting extends Api
{

	//设置活动金额
	public function money()
	{
		if (request()->isPost()) {
			// 获取数据
			$data = request()->post();
			if(!isset($data['sign']) || !isset($data['time'])) $this->error('参数错误');
			// 验签
			$sign = $data['sign'];
			$time = $data['time'];
			$now  = time() - (int)$time;
			if($now >= 60 || $now < -60) $this->error('时间格式错误', $now); // 时间对比
			$key = 'NdiWENBu-zzmind-PUBLIC';
			$mysign = md5($key.$time);
			
			if($mysign != $sign || !$sign) $this->error('签名错误');
			
			$config = json_decode(ModuleModel::where('name', 'activity')->value('config'), true);
			
			// 未传金额，默认为查询
			if(!isset($data['money'])){
				$money = RedpackModel::where('result_code', 'SUCCESS')->sum('total_amount');
				$this->success('查询成功', ['plan' => $config['money'], 'used' => $money/100]);
			} 
			
			$config['money'] = $config['money'] + (int)$data['money'];
			
			// 保存模块配置
			if (false === ModuleModel::where('name', 'activity')->update(['config' => json_encode($config)])) {
			    $this->error('设置失败');
			}
			// 非开发模式，缓存数据
			if (config('app.develop_mode') == 0) {
			    cache('module_config_activity', $config);
			}
			// 清除系统缓存
			cache('system_config', null);
			$this->success('设置成功', $config['money']);
		}else{
			$this->success('连接成功');
		}
	}
	//批量查询订单
	public function checkOrder()
	{
		// 设定30分钟前的时间戳
		$time = time() - 1800;
		$oids = OrderModel::where('create_time', '<', $time)->where('status', '=', 0)->order('update_time')->limit(50)->column('id');
		$success = count($oids);
		$error = 0;
		$del = 0;
		$order = new OrderModel;
		foreach ($oids as $id){
			$return = $order->checkOrder($id);
			if ($return === false) {
				$error++;
			} else {
				$del++;
			}
		}
		echo ('**************'."\r\n");
		echo ('***SUCCESS【'.$success.'】***，ERROR【'.$error.'】***，DELETE【'.$del.'】***'."\r\n");
		echo ('**************'."\r\n");
	}
	
	//确认收货
	public function actionOrder()
	{
		// 设定10分钟前的时间戳
		$time = time() - 3600 * 24 *7;
		$oids = OrderModel::where('send_time', '<', $time)->where('status', '=', 2)->column('id');
		$success = count($oids);
		$error = 0;
		$action = 0;
		$order = new OrderModel;
		foreach ($oids as $id){
			$return = $order->action($id);
			if ($return === false) {
				$error++;
			} else {
				$action++;
			}
		}
		echo ('**************'."\r\n");
		echo ('***SUCCESS【'.$success.'】***，ERROR【'.$error.'】***，ACTION【'.$action.'】***'."\r\n");
		echo ('**************'."\r\n");
		
	}
	
	//检查微信平台证书V3
	public function checkCert()
	{
		$time = time();
		$merch_ids = MerchModel::column('id');
		$return = [];
		if ($merch_ids) {
			foreach($merch_ids as $mid) {
				$cert = Cert::where('effective_time', '<', $time)->where('expire_time', '>', $time)->where('mid', $mid)->find();
				$dir = null;
				if($cert){
					if (is_file(root_path().$cert['dir'])) $dir = $cert['dir'];
				} 
				$getcert = new GetCert($dir, $mid);
				$return[] = $getcert->downloadCert();
			}
		}
		return json($return);
	}
}
